import yaml
import os

class DataLoader:
    def __init__(self):
        self.project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

    def get(self, file_path, key, default=None):
        full_path = os.path.join(self.project_root, file_path)
        if not os.path.exists(full_path):
            raise FileNotFoundError(f"数据文件不存在: {full_path}")
        with open(full_path, 'r', encoding='utf-8') as f:
            try:
                data = yaml.safe_load(f)
            except yaml.YAMLError as e:
                raise ValueError(f"YAML 解析错误 in {full_path}: {e}")
        # 支持嵌套 key（用点号分隔）
        keys = key.split('.')
        value = data
        for k in keys:
            if isinstance(value, dict) and k in value:
                value = value[k]
            else:
                return default
        return value

data_loader = DataLoader()